﻿
using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class SealController : Controller
    {
        private SealServers sealServers;

        public SealController()
        {
            sealServers = new SealServers();
        }


        // GET: System/Seal
        public ActionResult Index()
        {
            ViewBag.DEPT_IDData = JsonConvert.SerializeObject(sealServers.GetDEPT_ID());
            return View();
        }

        // GET: System/Seal/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.DEPT_IDData = sealServers.GetDEPT_ID();
            var model = new seal();
            if (id != null)
            {
                ViewBag.Title = "编辑电子印章表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建电子印章表";
            }
            return View(model);
        }

        public ActionResult SealDetails(int? id)
        {
            ViewBag.Title = "会议室设备明细";
            var model = new seal();
            model = GetModel(id.Value.ToString());
            model.DEPT_ID = sealServers.GetDEPT_IDName(model.DEPT_ID);
            return View(model);
        }

        // POST: System/Seal/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, seal model)
        {
            ViewBag.DEPT_IDData = sealServers.GetDEPT_ID();
            try
            {
                if (id == null || id == 0)
                {
                    sealServers.Add(model);
                    ViewBag.msg = "新增电子印章表信息成功！";
                    model = new seal();
                }
                else
                {
                    sealServers.Update(model);
                    ViewBag.msg = "修改电子印章表信息成功！";
                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: System/Seal/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                sealServers.Del(id.ToString());
                ViewBag.msg = "删除电子印章表信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult(string SEAL_ID, string SEAL_NAME)
        {
            var list = sealServers.GetData(SEAL_ID, SEAL_NAME);
            return JsonConvert.SerializeObject(list);
        }

        public seal GetModel(string id)
        {
            var model = sealServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}